/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import dao.UserDAO;
import dto_pakinfo.Profile;
import dto_pakinfo.User;
import dto_pakinfo.Users;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import util.ServerValidation;
import util.StringUtil;
import util.XMLUtil;

/**
 *
 * @author duonghung1369
 */
public class RegisterController extends BaseController {

    private String username;
    private String password;
    private String confirm_password;
    private String firstname;
    private String lastname;
    private String email;
    private String address;
    private String phone_number;
    private Log log;
    Users userUtility;
    private String userFilePath;

    public RegisterController() {
        log = LogFactory.getLog(RegisterController.class);
        userFilePath = System.getProperty("rootPath") + StringUtil.XML_FOLDER_PATH + "nonadminusers.xml";
        userUtility = new Users();
    }

    public String register() {
        UserDAO userDAO = new UserDAO(User.class);
        try {
            // server validation
            User user = new User();
            Profile profile = new Profile();
            user.setUsername(getUsername().trim());
            user.setPassword(getPassword().trim());
            //set created_date, updated date
            Date date = new Date();
            user.setCreateDate(XMLUtil.convertToSqlDate(date));
            user.setUpdateDate(XMLUtil.convertToSqlDate(date));
            profile.setFirstname(getFirstname().trim());
            profile.setLastname(getLastname().trim());
            profile.setEmail(getEmail().trim());
            profile.setAddress(getAddress().trim());
            profile.setPhoneNumber(getPhone_number().trim());
            user.setProfile(profile);
            profile.setUser(user);
            Map errors = ServerValidation.validateRegister(user);
            // if have any errors, return register page.
            if (errors.size() > 0) {
                Set<Map.Entry<String, String>> entries = errors.entrySet();
                for (Map.Entry<String, String> entry : entries) {
                    addActionError(entry.getValue());
                }
                return INPUT;
            }

            userDAO.openSession();
            boolean result = userDAO.register(user);
            if (!result) {
                addActionError("This account has already signed up, please try another one!");
                return INPUT;
            }
//            List<User> users = userDAO.all();
//            for (int i= 0; i < users.size(); i++) {
//                if(users.get(i).isIsAdmin()) {
//                    users.remove(users.get(i));
//                }
//            }
//            for (User userTemp : users) {
//                if (userTemp.isIsAdmin()) {
//                    users.remove(userTemp);
//                }
//            }
//            JAXBContext context = JAXBContext.newInstance(Users.class);
//            Users objUsers = new Users(users);
//                XMLUtil.marshalObject(context, objUsers, userFilePath);
            Document userDoc = XMLUtil.parserDOMXML(userFilePath);
            userUtility.addUserNode(userDoc, user);

            XMLUtil.writeXML(userDoc, userFilePath);
        } catch (Exception ex) {
            log.error(ex);
        } finally {
            userDAO.closeSession();
        }


        return SUCCESS;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the confirm_password
     */
    public String getConfirm_password() {
        return confirm_password;
    }

    /**
     * @param confirm_password the confirm_password to set
     */
    public void setConfirm_password(String confirm_password) {
        this.confirm_password = confirm_password;
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the phone_number
     */
    public String getPhone_number() {
        return phone_number;
    }

    /**
     * @param phone_number the phone_number to set
     */
    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }
}
